//
//  TTIGoalBar.m
//  iOSCodeProject
//
//  Created by Fox on 14-7-18.
//  Copyright (c) 2014年 翔傲信息科技（上海）有限公司. All rights reserved.
//

#import "TTIGoalBar.h"
#import "UIColor+Utils.h"

@implementation TTIGoalBar

@synthesize percentLayer = _percentLayer;


#pragma mark - Init
- (id)init{
    
	if ((self = [super init])){
		[self setup];
	}
    
	return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	if ((self = [super initWithCoder:aDecoder]))
	{
		[self setup];
	}
    
	return self;
}

- (id)initWithFrame:(CGRect)frame
{
	if ((self = [super initWithFrame:frame]))
	{
		[self setup];
	}
    
	return self;
}

-(void)setup {
    self.backgroundColor = [UIColor clearColor];
    self.clipsToBounds = NO;
    
    
    _percentLayer = [TTIGoalBarPercentLayer layer];
    _percentLayer.fromBottumCenter = NO;
    _percentLayer.contentsScale = [UIScreen mainScreen].scale;
    
    //默认参数
    _percentLayer.percent = 0;
    _percentLayer.innerRadius = self.bounds.size.width/2 - 5;
    _percentLayer.outerRadius = self.bounds.size.width/2;
    _percentLayer.fillColor = [UIColor colorForHex:@"3498db"];
    _percentLayer.defaultColor = [UIColor colorForHex:@"bbbbbb"];
    
    _percentLayer.frame = self.bounds;
    _percentLayer.masksToBounds = NO;
    [_percentLayer setNeedsDisplay];
    
    [self.layer addSublayer:_percentLayer];
    
    [self addSubview:self.topLabel];
    [self addSubview:self.bottomLabel];
    
}

-(void)layoutSubviews {
    //    CGRect frame = self.frame;
    //    int percent = percentLayer.percent * 100;
    //    [percentLabel setText:[NSString stringWithFormat:@"%i%%", percent]];
    
    [super layoutSubviews];
}

#pragma mark - Custom Getters/Setters
-(UILabel *)topLabel{
    
    if (!_topLabel) {
        CGRect frame = self.bounds;
        _topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, frame.size.width, frame.size.height/2)];
        _topLabel.backgroundColor = [UIColor clearColor];
        _topLabel.textAlignment = NSTextAlignmentCenter;
        _topLabel.font = [UIFont systemFontOfSize:12];
        _topLabel.textColor = [UIColor colorForHex:@"717171"];
    }
    
    return _topLabel;
}

-(UILabel *)bottomLabel{
    
    if (!_bottomLabel) {
        CGRect frame = self.bounds;
        _bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height/2 - 5,
                                                                 frame.size.width, frame.size.height/2)];
        _bottomLabel.backgroundColor = [UIColor clearColor];
        _bottomLabel.textAlignment = NSTextAlignmentCenter;
        _bottomLabel.font = [UIFont systemFontOfSize:15];
        _bottomLabel.textColor = [UIColor colorForHex:@"1abc9c"];
        _bottomLabel.text = @"0组";
    }
    
    return _bottomLabel;
}


- (void)setPercent:(int)percent animated:(BOOL)animated {
    
    CGFloat floatPercent = percent / 100.0;
    floatPercent = MIN(1, MAX(0, floatPercent));
    
    _percentLayer.percent = floatPercent;
    [self setNeedsLayout];
    [_percentLayer setNeedsDisplay];
    
}

-(void)setBarColor:(UIColor *)barColor{
    
    _barColor = barColor;
    
    self.percentLayer.fillColor = barColor;
    self.bottomLabel.textColor = barColor;
    
}

@end
